//
//  PywUserCenterFooterView.m
//  PywSdk
//
//  Created by smallwave on 16/4/9.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywUserCenterFooterView.h"
#import "UIButton+Pyw.h"
#import "UILabel+Pyw.h"
#import "UIImage+Pyw.h"
#import "PywMasonry.h"
#import "PywPlatformMain.h"
#import "PywCPNotifications.h"
#import "PywLogoutManger.h"
#import "PywLoginView.h"
#import "PywCommonManager.h"
#import "UILabel+PywAttributeTextTapAction.h"
#import "PywAppInfo.h"

@interface PywUserCenterFooterView ()<PywAttributeTapActionDelegate>

@property (nonatomic, strong) UILabel   *propmtLabel;   // 温馨提示
@property (nonatomic, strong) UIButton  *logoutBtn;     // 注销按钮
@property (nonatomic, strong) UILabel   *helpLabel;     // 帮助信息

@end

@implementation PywUserCenterFooterView

#pragma mark - override
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpSubviews];
    }
    return self;
}

#pragma mark - setUpSubviews
- (void)setUpSubviews{
    if ([PywPlatformMain sharedPlatformMain].accountType == UserType_Passport) {
        
        [self addSubview:self.logoutBtn];
        [self addSubview:self.helpLabel];
    } else {
        
        [self addSubview:self.propmtLabel];
        [self addSubview:self.logoutBtn];
        [self addSubview:self.helpLabel];
    }
}

#pragma mark - event method
- (void)logoutBtnClick{
    [self endEditing:YES];
    [[PywLogoutManger manger] logout:^{
        [[UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController dismissViewControllerAnimated:YES completion:^{
//            [[[PywLoginView alloc] init] addLogin2keyWindow];
        }];
    }];
}
#pragma mark - layoutSubviews
- (void)layoutSubviews{
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    if ([PywPlatformMain sharedPlatformMain].accountType == UserType_Passport) {
        
        [self.logoutBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.equalTo(weakSelf.mas_top).offset(30);
            make.width.equalTo(weakSelf.mas_width);
            make.height.equalTo(@40);
        }];
        
        [self.helpLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.equalTo(weakSelf.logoutBtn.mas_bottom).offset(20);
        }];
    } else {

        [self.propmtLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.equalTo(weakSelf.mas_top);
            make.bottom.equalTo(weakSelf.logoutBtn.mas_top);
            make.left.equalTo(weakSelf.mas_left).offset(20);
            make.right.equalTo(weakSelf.mas_right).offset(-20);
        }];
        
        [self.logoutBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.equalTo(weakSelf.mas_top).offset(40);
            make.width.equalTo(weakSelf.mas_width);
            make.height.equalTo(@40);
        }];
        
        [self.helpLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
            make.centerX.width.equalTo(weakSelf.logoutBtn);
            make.top.equalTo(weakSelf.logoutBtn.mas_bottom).offset(20);
        }];
    }
}
#pragma mark - PywAttributeTapActionDelegate
- (void)pyw_attributeTapReturnString:(NSString *)string range:(NSRange)range index:(NSInteger)index{
    NSString *phone = @"4000709394";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",phone]]];
}

#pragma mark - lazyLoad
- (UILabel *)propmtLabel{
    if (!_propmtLabel) {
        _propmtLabel = [UILabel labelWithText:@"温馨提示：用手机登录才能设置或修改交易密码"
                                textAlignment:NSTextAlignmentLeft
                                     fontSize:12
                                    textColor:[UIColor grayColor]];
    }
    return _propmtLabel;
}
- (UIButton *)logoutBtn{
    if (!_logoutBtn) {
        UIColor *titleColor = [PywAppInfo sharedAppInfo].appType == AppTypeFQ?UIColorFromRGB(0xFFFF5858):[UIColor blackColor];
        _logoutBtn = [UIButton buttonWithBackgroundColor:[UIColor whiteColor]
                                            cornerRadius:0
                                              titleColor:titleColor
                                      titleLabelFontSize:18
                                                   title:@"注 销"];
        
        [_logoutBtn addTarget:self action:@selector(logoutBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_logoutBtn setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
        [_logoutBtn setBackgroundImage:[self imageWithColor:RGB(204, 204, 204, 1)] forState:UIControlStateHighlighted];
        [_logoutBtn setAccessibilityIdentifier:@"center_logout"];
    }
    return _logoutBtn;
}
- (UILabel *)helpLabel{
    if (!_helpLabel) {
        _helpLabel = [UILabel labelWithText:nil
                                textAlignment:NSTextAlignmentCenter
                                     fontSize:16
                                    textColor:RGB(51, 51, 51, 1)];
        
        _helpLabel.attributedText = [self labelAttributedText];
        [_helpLabel pyw_addAttributeTapActionWithStrings:@[@"400-0709-394"] delegate:self];
    }
    return _helpLabel;
}
- (NSMutableAttributedString *)labelAttributedText{
    NSString *str = [NSString stringWithFormat:@"客服电话：400-0709-394  客服QQ：4000709394"];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
    
    CGFloat fontSize = [[PywCommonManager manager] changeDeviceTextFontSize:12];
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize] range:NSMakeRange(0, str.length)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(5, 12)];
    // 设置行高
    NSMutableParagraphStyle *sty = [[NSMutableParagraphStyle alloc] init];
    sty.alignment = NSTextAlignmentCenter;// 修改设置样式后，文本的对其方式也必须跟着修改，此时上面的无效
    sty.lineSpacing = 5;
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:sty range:NSMakeRange(0, str.length)];
    return attributedStr;
}

//  颜色转换为背景图片
- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
